package com.apes.fn.business.insurance.repository;

import com.apes.fn.business.insurance.modle.InsuranceOrder;
import com.apes.fn.business.insurance.modle.PolicyInformationItem;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 * @ClassName PolicyInformationItemRepository
 * @Author wsj
 * @Date 2021/6/21 15:35
 * @description 数据连接层
 */
public interface PolicyInformationItemRepository extends CustomRepository<PolicyInformationItem,String> {
    @Query("select t from PolicyInformationItem t where t.id in :ids ")
    List<PolicyInformationItem> findByIds(@Param("ids") List ids);

    @Modifying
    @Query(value = "update PolicyInformationItem t set t.settlement = :flag where t.policyInformation.id in :list and t.commissionType.name in :commType " )
    void updatePolicyInformationCommissionFlagByIds(@Param("flag") boolean flag, @Param("list") List list, @Param("commType") List commType);

    @Modifying
    @Query(value = "update policy_information_item" +
            "       set SETTLEMENT = :flag" +
            "       where ID in (select a.ID" +
            "                    from policy_information_item a," +
            "                         soe_business_attr_value b" +
            "                    where a.COMMISSION_TYPE = b.ID" +
            "                      and a.POLICY_INFORMATION_ID in (:list)" +
            "                      and b.name = :commType)",nativeQuery = true)
    void updatePolicyInformationItemSettlement(boolean flag, List list, String commType);

    @Query(value = "select t from PolicyInformationItem t where t.policyInformation.id in (:policyInformation) and t.settlement = :settlement and t.commissionType = :commissionType " )
    List<PolicyInformationItem> findAllBySettlementAndPolicyInformatioAndCommissionType(Boolean settlement, List policyInformation, BusinessAttributeValue commissionType);
    @Modifying
    @Query("UPDATE PolicyInformationItem t SET t.fixState = '1' WHERE t.id in :ids")
    int updatePolicyInformationItemFixStateToOne(@Param("ids") List ids);

    @Modifying
    @Query("UPDATE PolicyInformationItem t SET t.fixState = null WHERE t.id in :ids")
    int updatePolicyInformationItemFixStateToNull(@Param("ids") List ids);

    @Query("select t from PolicyInformationItem t where t.policyInformation.id = :id and t.commissionType.name = :commissionType")
    PolicyInformationItem findByPolicyAndCommissionType(@Param("id") String id,@Param("commissionType") String commissionType);
}
